package com.app.ui.activity.mood;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.net.res.mood.Mood;
import com.app.ui.activity.action.NormalActionBar;
import com.app.ui.dialog.DialogFunction;
import com.app.ui.dialog.OnDialogOptionListener;
import com.app.ui.event.MoodEvent;
import com.app.ui.pager.home.HomeMoodPager;
import com.app.utiles.other.ToastUtile;
import com.app.utiles.time.DatePicker;
import com.app.utiles.time.DateUtile;
import com.app.utiles.time.OnPickerDialogListener;
import com.wxj.zx.R;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 心情详情
 */
public class MoodDetailsActivity extends NormalActionBar {


    @Bind(R.id.record_data_tv)
    TextView recordDataTv;
    @Bind(R.id.record_context_et)
    EditText recordContextEt;

    private DatePicker datePicker;
    private Mood mood;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mood_add);
        setBarColor();
        setBarBack();
        ButterKnife.bind(this);
        //
        mood = (Mood) getObjectExtra("bean");
        datePicker = new DatePicker(this);
        datePicker.setOnPickerDialogListener(new TimeListener());
        recordContextEt.addTextChangedListener(new TextChangeListener());
        //
        setBarTvText(1, "备注心情");
        setBarTvText(2, "保存");
        recordContextEt.setText(mood.moodMsg);
        time = mood.moodTime.getTime();
        recordDataTv.setText(DateUtile.getDateFormat(mood.moodTime, DateUtile.DATA_FORMAT_YMD));

    }


    @Override
    protected void option() {
        String msg = recordContextEt.getText().toString();
        if (TextUtils.isEmpty(msg)) {
            ToastUtile.showToast("请输入您的备注");
            return;
        }
        mood.moodMsg = msg;
        mood.moodTime = new Date(time);
        dialogShow();
        mood.update(this, new UpdateDataListener());

    }


    private long time;
    private DialogFunction dialogFunction;

    @OnClick({R.id.record_data_tv, R.id.del_record_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.record_data_tv:
                //获取时间
                datePicker.show();
                break;
            case R.id.del_record_btn:
                if (dialogFunction == null) {
                    dialogFunction = new DialogFunction(this);
                    dialogFunction.setBtnsHint("取消", "确定");
                    dialogFunction.setTitle("提示");
                    dialogFunction.setMsgGravity(Gravity.CENTER);
                    dialogFunction.setOnDialogOptionListener(new DialogOptionListener());
                }
                dialogFunction.setMsg("确定删除？");
                dialogFunction.show();
                break;
        }
    }

    //监听时间
    class TimeListener implements OnPickerDialogListener {

        @Override
        public void onPickerDate(int type, int y, int m, int d) {
            recordDataTv.setText(y + "-" + ((m < 10) ? 0 : "") + m + "-" + ((d < 10) ? 0 : "") + d);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, y);
            calendar.set(Calendar.MONTH, m);
            calendar.set(Calendar.DATE, d);
            time = calendar.getTime().getTime();
        }

        @Override
        public void onPickerTime(int type, int h, int m) {

        }
    }

    //修改监听
    class UpdateDataListener extends UpdateListener {

        @Override
        public void onSuccess() {
            ToastUtile.showToast("修改成功");
            MoodEvent moodEvent = new MoodEvent();
            moodEvent.cls = HomeMoodPager.class;
            moodEvent.type = 2;
            EventBus.getDefault().post(moodEvent);
            dialogDismiss();
            finish();
        }

        @Override
        public void onFailure(int i, String s) {
            dialogDismiss();
            ToastUtile.showToast("修改失败");
        }
    }

    //删除监听
    class DeleteDataListener extends DeleteListener {

        @Override
        public void onSuccess() {
            ToastUtile.showToast("删除成功");
            MoodEvent moodEvent = new MoodEvent();
            moodEvent.cls = HomeMoodPager.class;
            moodEvent.type = 3;
            EventBus.getDefault().post(moodEvent);
            dialogDismiss();
            finish();
        }

        @Override
        public void onFailure(int i, String s) {
            dialogDismiss();
            ToastUtile.showToast("删除失败");
        }
    }

    class DialogOptionListener implements OnDialogOptionListener {


        @Override
        public void onDialogLeftClick(String... arg) {
            dialogFunction.dismiss();
        }

        @Override
        public void onDialogRightClick(String... arg) {
            dialogFunction.dismiss();
            dialogShow();
            mood.delete(MoodDetailsActivity.this, new DeleteDataListener());
        }

        @Override
        public void onDialogOtherClick(String... arg) {
            dialogFunction.dismiss();
        }
    }
}
